import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { NzTabPosition } from 'ng-zorro-antd/tabs';
import { ArticleInfoData, ColumnData, InformationService } from 'src/app/service/information.service';
import { appConfig } from '../../../config/app-config'
import { firstValueFrom } from 'rxjs';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-column-list',
  templateUrl: './column-list.component.html',
  styleUrls: ['./column-list.component.scss']
})
export class ColumnListComponent implements OnInit, OnDestroy {

  columnEmpty = false
  constructor(
    private information: InformationService,
    private route: ActivatedRoute,
    private router: Router
  ) { }

  tabPosition: NzTabPosition = 'left'
  ngOnInit() {
    this.getColumnCode()
    // this.loadData()
  }

  columnCode = ''
  getColumnCode(){
    this.route.queryParams.subscribe({
      next: (res: any) => {
        this.columnCode = res.code
        if(this.information.columnTree && this.information.columnTree.length > 0){
          this.getColumns()
        }else{
          console.log(222333)
          this.getColumnTree()
        }
      }
    })
  }

  columnList: ColumnData[] = []
  async getColumnTree(){
    try{
      const columnInfo: ColumnData = await firstValueFrom(this.information._getColumnInfo(appConfig.COLUMN_CODE))
      const columnTree: ColumnData[] = await firstValueFrom(this.information._getColumnList(columnInfo.storeId))
      
      console.log('columnInfo', columnInfo)
      console.log('columnTree', columnTree)
      
      this.information.columnTree = columnTree

      console.log(111)
      this.getColumns()
    }catch(error){
      
    }
  }

  // 获取子栏目信息
  getColumns(){
    this.columnCode = this.columnCode ? this.columnCode : this.information.columnTree[0].code
    const columns = this.information.columnTree.find(_ => _.code === this.columnCode)
    if(columns && columns.children && columns.children.length > 0){
      this.columnEmpty = false
      this.columnList = columns.children
      this.code = this.columnList[0].code

      this.getContentList()
    }else{
      this.columnEmpty = true
      this.columnList = []
      this.articleList = []
    }
  }

  code = ''
  page = 0
  size = 7
  articleList: ArticleInfoData[] = [];
  notice_default_cover = appConfig.NOTICE_DEFAULT_COVER
  getContentList(){
    this.articleList = []
    this.information._getContentList(this.code, this.page, this.size).subscribe({
      next: (res) => {
        console.log(res)
        // this.articleList = res
        if(res && res.data){
          this.articleList = res.data
        }
      },
      error: (err) => {
          
      },
    })
  }

  switchColumn(item: ColumnData){
    this.code = item.code
    this.page = 0
    this.size = 7
    this.getContentList()
  }

  goDetail(item: ArticleInfoData){
    this.router.navigate(['/information/detail'], { queryParams: { id: item.id } })
  }

  ngOnDestroy(): void {
      
  }

}
